package myky.com.doctorapp.utils;

import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;

import myky.com.doctorapp.base.CMApplication;

/**
 * @author Mr.li
 */
public class ShareUtil {


    private static SharedPreferences sharedPreferences = null;

    /**
     * 构造函数
     */
    public ShareUtil() {
        sharedPreferences = CMApplication.get().getSharedPreferences("SP_" + "Cmai", Context.MODE_PRIVATE);

    }

    public static SharedPreferences getInstance() {
        if (sharedPreferences == null) {
            new ShareUtil();
        }
        return sharedPreferences;
    }

    /**
     * 保存List
     *
     * @param tag
     * @param datalist
     */
    public static <T> void setDataList(String tag, ArrayList<T> datalist) {
        if (null == datalist || datalist.size() <= 0) {
            return;
        }

        Gson gson = new Gson();
        //转换成json数据，再保存
        String strJson = gson.toJson(datalist);
        getInstance().edit().clear();
        getInstance().edit().putString(tag, strJson);
        getInstance().edit().commit();

    }

    /**
     * 获取List
     *
     * @param tag
     * @return
     */
    public static <T> ArrayList<T> getDataList(String tag) {
        ArrayList<T> datalist = new ArrayList<T>();
        String strJson = sharedPreferences.getString(tag, null);
        if (null == strJson) {
            return datalist;
        }
        Gson gson = new Gson();
        datalist = gson.fromJson(strJson, new TypeToken<ArrayList<T>>() {
        }.getType());
        return datalist;

    }

    /**
     * 保存数据
     *
     * @param
     * @param key
     * @param value
     */
    public static void putData(String key, int value) {
        getInstance().edit().putInt(key, value).commit();
    }

    public static void putData(String key, long value) {
        getInstance().edit().putLong(key, value).commit();
    }

    public static void putData(String key, String value) {
        getInstance().edit().putString(key, value).commit();
    }

    public static void putData(String key, boolean value) {
        getInstance().edit().putBoolean(key, value).commit();
    }

    /**
     * 获取数据
     *
     * @param
     * @param key
     */
    public static String getString(String key) {
        return getInstance().getString(key, "");
    }

    public static int getInt(String key) {
        return getInstance().getInt(key, -1);
    }

    public static String getUserId(String key){

        int  v = (int) ((Math.random() * 9 + 1) * 1000);

        int anInt = getInstance().getInt(Constants.USERID, v);




        return anInt+"";

    }

    public static long getLong(String key) {
        return getInstance().getLong(key, 0L);
    }

    /**
     * 没有值默认是false
     *
     * @param
     * @param key
     * @return
     */
    public static boolean getBoolean(String key) {
        return getInstance().getBoolean(key, false);
    }

    /**
     * 没有值默认是 true
     *
     * @param
     * @param key
     * @return
     */
    public static boolean getBooleanTrue(String key) {
        return getInstance().getBoolean(key, true);
    }

    public static void clear() {
        getInstance().edit().clear().commit();
    }

}
